using System;
using Slaks.Graphs.Algorithms.DistanceAlgorithm;
using Slaks.Graphs.GraphBuilder;

namespace Slaks.Graphs.Algorithms.ExperimentalDistances
{
	public class MCSSubgraphVertexEdgeCount : GraphDistanceBase
	{
        public MCSSubgraphVertexEdgeCount()
            : base()
        {
        }

        public MCSSubgraphVertexEdgeCount(Graph one, Graph two) 
            : base ( one, two )
        {
        }
		
		public override bool CanOperateOnRepresentation (BuilderType builderType)
		{
			switch(builderType)
			{
			case BuilderType.NormalizedFrequencyRepresentationSchenker:
			case BuilderType.NormalizedFrequencyRepresentationSlaks:
			case BuilderType.RawFrequencyRepresentationSchenker:
			case BuilderType.RawFrequencyRepresentationSlaks:
				return true;
			}        
			return false;
		}
		/// <summary>
		/// MCSsubgraph
		/// </summary>
		/// <param name="one"></param>
		/// <param name="two"></param>
		/// <returns></returns>
		public override double GetDistance()
		{
			double mcsRegular = new DistMCS4VertexEdgeCount(one, two).GetDistance ();

			Graph maxCommonSubgraph = (one.GetMCS(two));
			double mcsVertexCount = maxCommonSubgraph.GetVertexCount();

			double graphSizeOneVertexCount = one.GetVertexCount();
			double graphSizeTwoVertexCount = two.GetVertexCount();

			double graphSizeOne = one.GetGraphSizeVertexEdgeCount;
			double graphSizeTwo = two.GetGraphSizeVertexEdgeCount;


			if (mcsVertexCount == graphSizeOneVertexCount)
			{
				return 1 - graphSizeOneVertexCount/graphSizeTwoVertexCount;
			}
			if (mcsVertexCount == graphSizeTwoVertexCount)
				return 1 - graphSizeTwoVertexCount/graphSizeOneVertexCount;
			if (mcsVertexCount*2 > graphSizeOneVertexCount && graphSizeOneVertexCount < graphSizeTwoVertexCount)
			{
				return 1 - graphSizeOne/Math.Max(graphSizeOne, graphSizeTwo);
			}
			else if (mcsVertexCount*2 > graphSizeTwoVertexCount && graphSizeOneVertexCount > graphSizeTwoVertexCount)
			{
				return 1 - graphSizeTwo/Math.Max(graphSizeOne, graphSizeTwo);
			}
			return mcsRegular;
		}
	}
}